﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Runtime.InteropServices;

namespace Homebrew_SD_Manager
{
    public partial class Main : Form
    {
        string version = "4.7";

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Update update = new Update();
            update.init_reseau(version);
            tabControl1.SelectedIndex = Homebrew_SD_Manager.Properties.Settings.Default.SettingTabPage;
            createDirectories();
            init_settings();
            init_homebrew();
            init_addHomebrew();
            init_SDWad();
            init_PCWad();
        }

        #region Fonctions
        private void createDirectories()
        {
            Directory.CreateDirectory("apps");
            Directory.CreateDirectory("wad");
        }

        private void Collect(int i)
        {
            GC.Collect();
            if (i == 0)
            {
                ilViewerSmall.Images.Clear();
                ilViewerLarge.Images.Clear();
            }
            else if (i == 1)
            {
                ilAddSmall.Images.Clear();
                ilAddLarge.Images.Clear();
            }
        }

        private void changeStatut(string statut, int maxPB)
        {
            lbSSStatut.Text = statut;
            pbSSprogressBar.Maximum = maxPB;
        }

        private void deleteSelectedHomebrew()
        {
            Collect(0);
            pbSSprogressBar.Value = 0;
            changeStatut("Suppression des homebrews sélectionnés", lvViewer.SelectedItems.Count);
            for (int i = 0; i < lvViewer.SelectedItems.Count; i++)
            {
                Directory.Delete(@"apps\" + lvViewer.SelectedItems[i].Text, true);
                pbSSprogressBar.Value++;
            }
            init_homebrew();
            lbSSStatut.Text = "Suppression des homebrews terminés !";
        }

        private void deleteAllHomebrew()
        {
            Collect(0);
            pbSSprogressBar.Value = 0;
            changeStatut("Suppression des homebrews sélectionnés", lvViewer.Items.Count);
            for (int i = 0; i < lvViewer.Items.Count; i++)
            {
                Directory.Delete(@"apps\" + lvViewer.Items[i].Text, true);
                pbSSprogressBar.Value++;
            }
            lvViewer.Items.Clear();
            lbSSStatut.Text = "Suppression des homebrews terminé !";
        }

        private void refreshList()
        {
            init_homebrew();
        }

        private void openWindows(string s)
        {
            if (s == "sd")
            {
                System.Diagnostics.Process.Start(Environment.CurrentDirectory);
                Close();
            }
            else if (s == "bootmii")
            {
                if (Directory.Exists("bootmii") || Directory.Exists("~bootmii"))
                {
                    BootMii bootmii = new BootMii();
                    bootmii.ShowDialog();
                }
                else
                    errorBox("Le dossier \"bootmii\" n'existe pas sur la carte SD, veuillez préparer votre carte SD avec BootMii avant d'en modifier les options.");
            }
            else if (s == "roms")
            {
                Roms roms = new Roms();
                roms.ShowDialog();
            }
            else if (s == "editor")
            {
                EditorMeta em = new EditorMeta();
                em.ShowDialog();
            }
        }

        private void errorBox(string text)
        {
            MessageBox.Show(text, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void addHomebrew()
        {
            Collect(0);
            Collect(1);
            pbSSprogressBar.Value = 0;
            changeStatut("Ajout des homebrew en cours . . .", lvAdd.CheckedItems.Count);
            for (int i = 0; i < lvAdd.CheckedItems.Count; i++)
            {
                Directory.CreateDirectory(@"apps\" + lvAdd.CheckedItems[i].Text);
                DirectoryInfo dInfo = new DirectoryInfo(tbPath.Text + @"\" + lvAdd.CheckedItems[i].Text);
                foreach (FileInfo f in dInfo.GetFiles())
                    f.CopyTo(@"apps\" + lvAdd.CheckedItems[i].Text + @"\" + f.ToString(), true);
                pbSSprogressBar.Value++;
            }
            init_addHomebrew();
            init_homebrew();
            cbSelectAll.Checked = false;
            lbSSStatut.Text = "Ajout des homebrews terminé !";
        }
        #endregion

        #region Load
        private void init_back(int color)
        {
            if (color != 0)
            {
                BackColor = Color.FromArgb(color);
                menuStrip1.BackColor = Color.FromArgb(color);
                affichageToolStripMenuItem.BackColor = Color.FromArgb(color);
                ajouterÀLaCarteSDToolStripMenuItem.BackColor = Color.FromArgb(color);
                ajouteurToolStripMenuItem.BackColor = Color.FromArgb(color);
                àProposDHSDMToolStripMenuItem.BackColor = Color.FromArgb(color);
                editionToolStripMenuItem.BackColor = Color.FromArgb(color);
                éditeurDeFichierMetaxmlToolStripMenuItem.BackColor = Color.FromArgb(color);
                effacerSélectionnésToolStripMenuItem.BackColor = Color.FromArgb(color);
                fichierToolStripMenuItem.BackColor = Color.FromArgb(color);
                gestionsRomsToolStripMenuItem.BackColor = Color.FromArgb(color);
                grandesIconesToolStripMenuItem.BackColor = Color.FromArgb(color);
                listeToolStripMenuItem.BackColor = Color.FromArgb(color);
                optionsBootMiiToolStripMenuItem.BackColor = Color.FromArgb(color);
                outilsToolStripMenuItem.BackColor = Color.FromArgb(color);
                ouvrirLaCarteSDToolStripMenuItem.BackColor = Color.FromArgb(color);
                quitterToolStripMenuItem.BackColor = Color.FromArgb(color);
                rafraichirLaListeToolStripMenuItem.BackColor = Color.FromArgb(color);
                toolStripMenuItem1.BackColor = Color.FromArgb(color);
                toutEffacerToolStripMenuItem.BackColor = Color.FromArgb(color);
                visionneuseToolStripMenuItem.BackColor = Color.FromArgb(color);
                toolStrip1.BackColor = Color.FromArgb(color);
                tpAdd.BackColor = Color.FromArgb(color);
                tpSettings.BackColor = Color.FromArgb(color);
                tpViewer.BackColor = Color.FromArgb(color);
                tpWad.BackColor = Color.FromArgb(color);
                tbCoder.BackColor = Color.FromArgb(color);
                tbDescription.BackColor = Color.FromArgb(color);
                tbName.BackColor = Color.FromArgb(color);
                tbPath.BackColor = Color.FromArgb(color);
                tbPathWad.BackColor = Color.FromArgb(color);
                tbSettingOpacity.BackColor = Color.FromArgb(color);
                tbSettingPath.BackColor = Color.FromArgb(color);
                tbSettingPath2.BackColor = Color.FromArgb(color);
                tbVersion.BackColor = Color.FromArgb(color);
                lvAdd.BackColor = Color.FromArgb(color);
                lvComputerWad.BackColor = Color.FromArgb(color);
                lvSDCardWad.BackColor = Color.FromArgb(color);
                lvViewer.BackColor = Color.FromArgb(color);
                btCreateAutorun.BackColor = Color.FromArgb(color);
                btDeleteWad.BackColor = Color.FromArgb(color);
                btPath.BackColor = Color.FromArgb(color);
                btPathWad.BackColor = Color.FromArgb(color);
                btSave.BackColor = Color.FromArgb(color);
                btSettingPath.BackColor = Color.FromArgb(color);
                btSettingPath2.BackColor = Color.FromArgb(color);
                btToPC.BackColor = Color.FromArgb(color);
                btToSD.BackColor = Color.FromArgb(color);
                cbSelectAll.BackColor = Color.FromArgb(color);
                cbSelectAll2.BackColor = Color.FromArgb(color);
                cbSelectAll3.BackColor = Color.FromArgb(color);
                cbSettingListView.BackColor = Color.FromArgb(color);
                cbSettingTabPage.BackColor = Color.FromArgb(color);
                statusStrip1.BackColor = Color.FromArgb(color);
                label10.BackColor = Color.FromArgb(color);
                label11.BackColor = Color.FromArgb(color);
                tbSettingBackColor.BackColor = Color.FromArgb(color);
                tbSettingForeColor.BackColor = Color.FromArgb(color);
                button1.BackColor = Color.FromArgb(color);
                button2.BackColor = Color.FromArgb(color);
            }
        }

        private void init_fore(int color)
        {
            if (color != 0)
            {
                menuStrip1.ForeColor = Color.FromArgb(color);
                affichageToolStripMenuItem.ForeColor = Color.FromArgb(color);
                ajouterÀLaCarteSDToolStripMenuItem.ForeColor = Color.FromArgb(color);
                ajouteurToolStripMenuItem.ForeColor = Color.FromArgb(color);
                àProposDHSDMToolStripMenuItem.ForeColor = Color.FromArgb(color);
                editionToolStripMenuItem.ForeColor = Color.FromArgb(color);
                éditeurDeFichierMetaxmlToolStripMenuItem.ForeColor = Color.FromArgb(color);
                effacerSélectionnésToolStripMenuItem.ForeColor = Color.FromArgb(color);
                fichierToolStripMenuItem.ForeColor = Color.FromArgb(color);
                gestionsRomsToolStripMenuItem.ForeColor = Color.FromArgb(color);
                grandesIconesToolStripMenuItem.ForeColor = Color.FromArgb(color);
                listeToolStripMenuItem.ForeColor = Color.FromArgb(color);
                optionsBootMiiToolStripMenuItem.ForeColor = Color.FromArgb(color);
                outilsToolStripMenuItem.ForeColor = Color.FromArgb(color);
                ouvrirLaCarteSDToolStripMenuItem.ForeColor = Color.FromArgb(color);
                quitterToolStripMenuItem.ForeColor = Color.FromArgb(color);
                rafraichirLaListeToolStripMenuItem.ForeColor = Color.FromArgb(color);
                toolStripMenuItem1.ForeColor = Color.FromArgb(color);
                toutEffacerToolStripMenuItem.ForeColor = Color.FromArgb(color);
                visionneuseToolStripMenuItem.ForeColor = Color.FromArgb(color);
                toolStrip1.ForeColor = Color.FromArgb(color);
                tpAdd.ForeColor = Color.FromArgb(color);
                tpSettings.ForeColor = Color.FromArgb(color);
                tpViewer.ForeColor = Color.FromArgb(color);
                tpWad.ForeColor = Color.FromArgb(color);
                tbCoder.ForeColor = Color.FromArgb(color);
                tbDescription.ForeColor = Color.FromArgb(color);
                tbName.ForeColor = Color.FromArgb(color);
                tbPath.ForeColor = Color.FromArgb(color);
                tbPathWad.ForeColor = Color.FromArgb(color);
                tbSettingOpacity.ForeColor = Color.FromArgb(color);
                tbSettingPath.ForeColor = Color.FromArgb(color);
                tbSettingPath2.ForeColor = Color.FromArgb(color);
                tbVersion.ForeColor = Color.FromArgb(color);
                lvAdd.ForeColor = Color.FromArgb(color);
                lvComputerWad.ForeColor = Color.FromArgb(color);
                lvSDCardWad.ForeColor = Color.FromArgb(color);
                lvViewer.ForeColor = Color.FromArgb(color);
                btCreateAutorun.ForeColor = Color.FromArgb(color);
                btDeleteWad.ForeColor = Color.FromArgb(color);
                btPath.ForeColor = Color.FromArgb(color);
                btPathWad.ForeColor = Color.FromArgb(color);
                btSave.ForeColor = Color.FromArgb(color);
                btSettingPath.ForeColor = Color.FromArgb(color);
                btSettingPath2.ForeColor = Color.FromArgb(color);
                btToPC.ForeColor = Color.FromArgb(color);
                btToSD.ForeColor = Color.FromArgb(color);
                cbSelectAll.ForeColor = Color.FromArgb(color);
                cbSelectAll2.ForeColor = Color.FromArgb(color);
                cbSelectAll3.ForeColor = Color.FromArgb(color);
                cbSettingListView.ForeColor = Color.FromArgb(color);
                cbSettingTabPage.ForeColor = Color.FromArgb(color);
                gbGeneral.ForeColor = Color.FromArgb(color);
                gbSkin.ForeColor = Color.FromArgb(color);
                label1.ForeColor = Color.FromArgb(color);
                label2.ForeColor = Color.FromArgb(color);
                label3.ForeColor = Color.FromArgb(color);
                label4.ForeColor = Color.FromArgb(color);
                label5.ForeColor = Color.FromArgb(color);
                statusStrip1.ForeColor = Color.FromArgb(color);
                label10.ForeColor = Color.FromArgb(color);
                label11.ForeColor = Color.FromArgb(color);
                tbSettingBackColor.ForeColor = Color.FromArgb(color);
                tbSettingForeColor.ForeColor = Color.FromArgb(color);
                button1.ForeColor = Color.FromArgb(color);
                button2.ForeColor = Color.FromArgb(color);
            }
        }

        private void init_settings()
        {
            try
            {
                tbPath.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingHPath;
                tbPathWad.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingWPath;
                if (Homebrew_SD_Manager.Properties.Settings.Default.SettingOpacity > 0.4 && Homebrew_SD_Manager.Properties.Settings.Default.SettingOpacity <= 1.0)
                Opacity = Homebrew_SD_Manager.Properties.Settings.Default.SettingOpacity;
                if (Homebrew_SD_Manager.Properties.Settings.Default.SettingListView == 0)
                {
                    lvAdd.View = View.List;
                    lvViewer.View = View.List;
                }
                else
                {
                    lvAdd.View = View.LargeIcon;
                    lvViewer.View = View.LargeIcon;
                }
                init_back(Homebrew_SD_Manager.Properties.Settings.Default.SettingBackColor);
                init_fore(Homebrew_SD_Manager.Properties.Settings.Default.SettingForeColor);

                cbSettingTabPage.SelectedIndex = Homebrew_SD_Manager.Properties.Settings.Default.SettingTabPage;
                cbSettingListView.SelectedIndex = Homebrew_SD_Manager.Properties.Settings.Default.SettingListView;
                tbSettingPath.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingHPath;
                tbSettingPath2.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingWPath;
                tbSettingOpacity.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingOpacity.ToString();
                tbSettingBackColor.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingBackColor.ToString();
                tbSettingForeColor.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingForeColor.ToString();
            }
            catch { }
        }

        private void init_homebrew()
        {
            Collect(0);
            lvViewer.Items.Clear();
            DirectoryInfo dInfo = new DirectoryInfo(@"apps");
            pbSSprogressBar.Value = 0;
            changeStatut("Chargement des homebrews . . .", dInfo.GetDirectories().Count());
            foreach (DirectoryInfo di in dInfo.GetDirectories())
            {
                if (File.Exists(di.FullName + @"\boot.dol") || File.Exists(di.FullName + @"\boot.elf") || File.Exists(di.FullName + @"\theme.zip"))
                    lvViewer.Items.Add(di.Name.ToString());
                pbSSprogressBar.Value++;
            }

            for (int i = 0; i < lvViewer.Items.Count; i++)
            {
                if (File.Exists(@"apps\" + lvViewer.Items[i].Text + @"\icon.png"))
                {
                    ilViewerLarge.Images.Add(Image.FromFile(@"apps\" + lvViewer.Items[i].Text + @"\icon.png"));
                    ilViewerSmall.Images.Add(Image.FromFile(@"apps\" + lvViewer.Items[i].Text + @"\icon.png"));
                }
                else
                {
                    ilViewerLarge.Images.Add(Homebrew_SD_Manager.Properties.Resources.No_Image);
                    ilViewerSmall.Images.Add(Homebrew_SD_Manager.Properties.Resources.No_Image);
                }
                lvViewer.Items[i].ImageIndex = i;
            }
            if (lvViewer.Items.Count != 0)
                lvViewer.Items[0].Selected = true;
            lbSSStatut.Text = "Chargement des homebrews terminés !";
        }

        private void init_addHomebrew()
        {
            if (tbPath.Text != "")
            {
                Collect(1);
                lvAdd.Items.Clear();
                DirectoryInfo dInfo = new DirectoryInfo(tbPath.Text);
                pbSSprogressBar.Value = 0;
                changeStatut("Chargement des homebrews . . .", dInfo.GetDirectories().Count());
                foreach (DirectoryInfo di in dInfo.GetDirectories())
                {
                    if (File.Exists(tbPath.Text + @"\" + di.Name + @"\boot.dol") || File.Exists(tbPath.Text + @"\" + di.Name + @"\boot.elf") || File.Exists(tbPath.Text + @"\" + di.Name + @"\theme.zip"))
                        lvAdd.Items.Add(di.Name.ToString());
                    pbSSprogressBar.Value++;
                }

                for (int i = 0; i < lvAdd.Items.Count; i++)
                {
                    if (File.Exists(tbPath.Text + @"\" + lvAdd.Items[i].Text + @"\icon.png"))
                    {
                        ilAddLarge.Images.Add(Image.FromFile(tbPath.Text + @"\" + lvAdd.Items[i].Text + @"\icon.png"));
                        ilAddSmall.Images.Add(Image.FromFile(tbPath.Text + @"\" + lvAdd.Items[i].Text + @"\icon.png"));
                    }
                    else
                    {
                        ilAddLarge.Images.Add(Homebrew_SD_Manager.Properties.Resources.No_Image);
                        ilAddSmall.Images.Add(Homebrew_SD_Manager.Properties.Resources.No_Image);
                    }
                    lvAdd.Items[i].ImageIndex = i;
                }
                lbSSStatut.Text = "Chargement des homebrews terminés !";
            }
        }

        private void init_PCWad()
        {
            if (tbPathWad.Text != "")
            {
                lvComputerWad.Items.Clear();
                DirectoryInfo dInfo = new DirectoryInfo(tbPathWad.Text);
                pbSSprogressBar.Value = 0;
                changeStatut("Chargement des wads . . .", dInfo.GetFiles().Count());
                foreach (FileInfo f in dInfo.GetFiles())
                {
                    if (f.Extension == ".wad")
                        lvComputerWad.Items.Add(f.Name);
                    pbSSprogressBar.Value++;
                }
                lbSSStatut.Text = "Chargement des wads terminés !";
            }
        }

        private void init_SDWad()
        {
            lvSDCardWad.Items.Clear();
            DirectoryInfo dInfo = new DirectoryInfo(@"wad");
            pbSSprogressBar.Value = 0;
            changeStatut("Chargement des wads . . .", dInfo.GetFiles().Count());
            foreach (FileInfo f in dInfo.GetFiles())
            {
                if (f.Extension == ".wad")
                    lvSDCardWad.Items.Add(f.Name);
                pbSSprogressBar.Value++;
            }
            if (lvSDCardWad.Items.Count == 0)
            {
                btDeleteWad.Enabled = false;
                btToPC.Enabled = false;
            }
            lbSSStatut.Text = "Chargement des wads terminés !";
        }
        #endregion

        #region Visionneuse
        private void lvViewer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvViewer.SelectedItems.Count != 0)
            {
                string xmlPath = @"apps\" + lvViewer.SelectedItems[0].Text + @"\meta.xml";
                if (File.Exists(xmlPath))
                {
                    XmlTextReader xtr = new XmlTextReader(xmlPath);

                    while (xtr.Read())
                    {
                        if (xtr.LocalName == "name")
                            tbName.Text = xtr.ReadString();
                        else if (xtr.LocalName == "version")
                            tbVersion.Text = xtr.ReadString();
                        else if (xtr.LocalName == "coder")
                            tbCoder.Text = xtr.ReadString();
                        else if (xtr.LocalName == "long_description")
                            tbDescription.Text = xtr.ReadString();
                    }
                    xtr.Close();
                }

                else
                {
                    tbName.Text = "null";
                    tbVersion.Text = "null";
                    tbCoder.Text = "null";
                    tbDescription.Text = "null";
                    lbSSStatut.Text = "Fichier meta.xml manquant pour " + lvViewer.SelectedItems[0].Text;
                }
            }

            if (lvViewer.SelectedItems.Count != 0)
            {
                effacerSélectionnésToolStripMenuItem.Enabled = true;
                tsbDeleteSelected.Enabled = true;
            }
            else
            {
                effacerSélectionnésToolStripMenuItem.Enabled = false;
                tsbDeleteSelected.Enabled = false;
            }
        }
        #endregion

        #region Ajouteur
        private void btPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK && Environment.CurrentDirectory.Substring(0, 3) != fbd.SelectedPath.Substring(0, 3))
            {
                tbPath.Text = fbd.SelectedPath;
                Homebrew_SD_Manager.Properties.Settings.Default.SettingHPath = fbd.SelectedPath;
                Homebrew_SD_Manager.Properties.Settings.Default.Save();
                tbSettingPath.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingHPath;
                init_addHomebrew();
            }
        }

        private void cbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSelectAll.Checked == true)
            {
                for (int i = 0; i < lvAdd.Items.Count; i++)
                    lvAdd.Items[i].Checked = true;

            }
            else
            {

                for (int i = 0; i < lvAdd.Items.Count; i++)
                    lvAdd.Items[i].Checked = false;
            }
        }

        private void lvAdd_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (lvAdd.CheckedItems.Count != 0)
            {
                ajouterÀLaCarteSDToolStripMenuItem.Enabled = true;
                tsbAddToSDCard.Enabled = true;
            }
            else
            {
                ajouterÀLaCarteSDToolStripMenuItem.Enabled = false;
                tsbAddToSDCard.Enabled = false;
            }
        }
        #endregion

        #region Wad
        private void cbSelectAll2_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSelectAll2.Checked == true)
            {
                for (int i = 0; i < lvComputerWad.Items.Count; i++)
                    lvComputerWad.Items[i].Checked = true;

            }
            else
            {

                for (int i = 0; i < lvComputerWad.Items.Count; i++)
                    lvComputerWad.Items[i].Checked = false;
            }
        }

        private void cbSelectAll3_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSelectAll3.Checked == true)
            {
                for (int i = 0; i < lvSDCardWad.Items.Count; i++)
                    lvSDCardWad.Items[i].Checked = true;

            }
            else
            {

                for (int i = 0; i < lvSDCardWad.Items.Count; i++)
                    lvSDCardWad.Items[i].Checked = false;
            }
        }

        private void btPathWad_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK && Environment.CurrentDirectory.Substring(0, 3) != fbd.SelectedPath.Substring(0, 3))
            {
                tbPathWad.Text = fbd.SelectedPath;
                Homebrew_SD_Manager.Properties.Settings.Default.SettingWPath = fbd.SelectedPath;
                Homebrew_SD_Manager.Properties.Settings.Default.Save();
                tbSettingPath2.Text = Homebrew_SD_Manager.Properties.Settings.Default.SettingWPath;
                init_PCWad();
            }
        }

        private void btToSD_Click(object sender, EventArgs e)
        {
            pbSSprogressBar.Value = 0;
            changeStatut("Ajout des wads . . .", lvComputerWad.CheckedItems.Count);
            for (int i = 0; i < lvComputerWad.CheckedItems.Count; i++)
            {
                File.Copy(tbPathWad.Text + @"\" + lvComputerWad.CheckedItems[i].Text, @"wad\" + lvComputerWad.CheckedItems[i].Text, true);
                pbSSprogressBar.Value++;
            }
            init_SDWad();
            cbSelectAll2.Checked = false;
            cbSelectAll3.Checked = false;
            lbSSStatut.Text = "Ajout des wads terminé !";
        }

        private void btToPC_Click(object sender, EventArgs e)
        {
            if (tbPathWad.Text != "")
            {
                pbSSprogressBar.Value = 0;
                changeStatut("Ajout des wads . . .", lvSDCardWad.CheckedItems.Count);
                for (int i = 0; i < lvSDCardWad.CheckedItems.Count; i++)
                {
                    File.Copy(@"wad\" + lvSDCardWad.CheckedItems[i].Text, tbPathWad.Text + @"\" + lvSDCardWad.CheckedItems[i].Text, true);
                    pbSSprogressBar.Value++;
                }
                init_PCWad();
                cbSelectAll2.Checked = false;
                cbSelectAll3.Checked = false;
                lbSSStatut.Text = "Ajout des wads terminé !";
            }
            else
                errorBox("Veuillez selectionner un dossier où l'envoyer");
        }

        private void btDeleteWad_Click(object sender, EventArgs e)
        {
            pbSSprogressBar.Value = 0;
            changeStatut("Suppression des wads sélectionné . . .", lvSDCardWad.CheckedItems.Count);
            for (int i = 0; i < lvSDCardWad.CheckedItems.Count; i++)
            {
                File.Delete(@"wad\" + lvSDCardWad.CheckedItems[i].Text);
                pbSSprogressBar.Value++;
            }
            init_SDWad();
            cbSelectAll2.Checked = false;
            cbSelectAll3.Checked = false;
            lbSSStatut.Text = "Suppression des wads terminé !";
        }

        private void lvComputerWad_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (lvComputerWad.CheckedItems.Count != 0)
                btToSD.Enabled = true;
            else
                btToSD.Enabled = false;
        }

        private void lvSDCardWad_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (lvSDCardWad.CheckedItems.Count != 0)
            {
                btToPC.Enabled = true;
                btDeleteWad.Enabled = true;
            }
            else
            {
                btToPC.Enabled = false;
                btDeleteWad.Enabled = false;
            }
        }
        #endregion

        #region Options
        private void btSettingPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK && Environment.CurrentDirectory.Substring(0, 3) != fbd.SelectedPath.Substring(0, 3))
                tbSettingPath.Text = fbd.SelectedPath;
        }

        private void btSettingPath2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK && Environment.CurrentDirectory.Substring(0, 3) != fbd.SelectedPath.Substring(0, 3))
                tbSettingPath2.Text = fbd.SelectedPath;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            Homebrew_SD_Manager.Properties.Settings.Default.SettingHPath = tbSettingPath.Text;
            Homebrew_SD_Manager.Properties.Settings.Default.SettingWPath = tbSettingPath2.Text;
            Homebrew_SD_Manager.Properties.Settings.Default.SettingOpacity = Double.Parse(tbSettingOpacity.Text);
            Homebrew_SD_Manager.Properties.Settings.Default.SettingTabPage = cbSettingTabPage.SelectedIndex;
            Homebrew_SD_Manager.Properties.Settings.Default.SettingListView = cbSettingListView.SelectedIndex;
            if (tbSettingBackColor.Text != "")
                Homebrew_SD_Manager.Properties.Settings.Default.SettingBackColor = Int32.Parse(tbSettingBackColor.Text);
            if (tbSettingForeColor.Text != "")
                Homebrew_SD_Manager.Properties.Settings.Default.SettingForeColor = Int32.Parse(tbSettingForeColor.Text);
            Homebrew_SD_Manager.Properties.Settings.Default.Save();
            init_settings();
            init_addHomebrew();
            init_PCWad();
            init_back(Homebrew_SD_Manager.Properties.Settings.Default.SettingBackColor);
            init_fore(Homebrew_SD_Manager.Properties.Settings.Default.SettingForeColor);
            lbSSStatut.Text = "Options enregistrées avec succès !";
        }

        private void createAutorun_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter("autorun.inf");
            sw.WriteLine("[autorun]");
            sw.WriteLine("icon=homebrewsdmanager.exe,0");
            sw.WriteLine("open=homebrewsdmanager.exe");
            sw.WriteLine("label=Homebrew SD Manager");
            sw.WriteLine("action=Homebrew SD Manager");
            sw.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
                tbSettingBackColor.Text = cd.Color.ToArgb().ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
                tbSettingForeColor.Text = cd.Color.ToArgb().ToString();
        }
        #endregion

        #region Menu
        private void effacerSélectionnésToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deleteSelectedHomebrew();
        }

        private void toutEffacerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deleteAllHomebrew();
        }

        private void rafraichirLaListeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshList();
        }

        private void ouvrirLaCarteSDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openWindows("sd");
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void listeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lvViewer.View = View.List;
            lvAdd.View = View.List;
        }

        private void grandesIconesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lvViewer.View = View.LargeIcon;
            lvAdd.View = View.LargeIcon;
        }

        private void ajouterÀLaCarteSDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addHomebrew();
        }

        private void àProposDHSDMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Homebrew SD Manager {0}\nBy GoldenSun2".Replace("{0}", version), "A propos de . . . ", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void optionsBootMiiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openWindows("bootmii");
        }

        private void gestionsRomsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openWindows("roms");
        }

        private void éditeurDeFichierMetaxmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openWindows("editor");
        }

        private void theHDownloaderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (File.Exists("thehdownloader.exe"))
            {
                System.Diagnostics.Process.Start("thehdownloader.exe");
                System.Threading.Thread.Sleep(1000);
                Close();
            }
            else
                MessageBox.Show("The HDownloader n'a pas été trouvé sur la carte SD");
        }

        private void téléchargerTheHDownloaderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            changeStatut("Téléchargement de The HDownloader . . .", 100);
            Uri url = new Uri("http://homebrewsdmanager.googlecode.com/svn/trunk/update/THD.exe");
            System.Net.WebClient client = new System.Net.WebClient();
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFileAsync(url, "thehdownloader.exe");
        }
        #endregion

        #region Raccourci
        private void tsbDeleteSelected_Click(object sender, EventArgs e)
        {
            deleteSelectedHomebrew();
        }

        private void tsbDeleteAll_Click(object sender, EventArgs e)
        {
            deleteAllHomebrew();
        }

        private void tsbRefresh_Click(object sender, EventArgs e)
        {
            refreshList();
        }

        private void tsbOpenCardSD_Click(object sender, EventArgs e)
        {
            openWindows("sd");
        }

        private void tsbAddToSDCard_Click(object sender, EventArgs e)
        {
            addHomebrew();
        }
        #endregion

        #region Download THD
        void client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            pbSSprogressBar.Value = e.ProgressPercentage;
        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Téléchargement terminé !");
        }
        #endregion
    }
}